#include <string.h>
#include "lib_jpeg_adapt.h"
#include "lib_png_adapt.h"
#include "util_bmp.h"
#include "image_format.h"

static char* GetTail(char* file)
{
    int32_t len = strlen(file);
    char* ret = NULL;
    do {
        ret = &file[--len];
        if (*ret == '.')
            return ret + 1;
    } while (len > 0);
    return ret;
}

Image UtilPicGet(const char* file)
{
    Image image = {0};
    if (!file)
        return image;
    char *tail = GetTail((char*)file);
    if (strlen(tail) >= 3)
    {
        if ((tail[0] == 'j' || tail[0] == 'J') && (tail[1] == 'p' || tail[1] == 'P'))
            image.mem.addr = LibJpegGetBGRA(file, &image.info.width, &image.info.height, &image.info.pb);
        else if ((tail[0] == 'p' || tail[0] == 'P') && (tail[1] == 'n' || tail[1] == 'N'))
            image.mem.addr = LibPngGet(file, &image.info.width, &image.info.height, &image.info.pb, LPT_BGRA);
        else if ((tail[0] == 'b' || tail[0] == 'B') && (tail[1] == 'm' || tail[1] == 'M'))
            image.mem.addr = UtilBmpGetBGRA(file, &image.info.width, &image.info.height, &image.info.pb);
        image.info.format = IMAGE_FORMAT_BGRA;
        image.info.size = image.info.width * image.info.height * image.info.pb;
    }
    return image;
}

Image UtilPicGet(const char* file, ImageFormat format, uint32_t width, uint32_t height)
{
    Image image = UtilPicGet(file);
    if (!image.mem.addr)
        return image;

    Image tImage;
    if ((width == 0 || width == image.info.width) && 
        (height == 0 || height == image.info.height)) {
        if (format != image.info.format)
            tImage = ImageTranslate(image, image.info.width, image.info.height, format);
        else
            return image;
    }
    else if (width > 0 && height > 0) {
        tImage = ImageTranslate(image, width, height, format);
    }
    else if (width > 0) {
        height = width * image.info.height / image.info.width;
        tImage = ImageTranslate(image, width, height, format);
    }
    else {
        width = height * image.info.width / image.info.height;
        tImage = ImageTranslate(image, width, height, format);
    }

    ImageDestory(image);
    return tImage;
}

void UtilPicCreate(const char* file, Image image)
{
    if (!file || !image.mem.addr)
        return;
    
    char *tail = GetTail((char*)file);
    if (strlen(tail) >= 3)
    {
        if ((tail[0] == 'j' || tail[0] == 'J') && (tail[1] == 'p' || tail[1] == 'P'))
            LibJpegCreate(file, image.mem.addr, image.info.width, image.info.height, image.info.pb, 75);
        else if ((tail[0] == 'b' || tail[0] == 'B') && (tail[1] == 'm' || tail[1] == 'M'))
            UtilBmpCreate(file, image.mem.addr, image.info.width, image.info.height, image.info.pb);
        else //if ((tail[0] == 'p' || tail[0] == 'P') && (tail[1] == 'n' || tail[1] == 'N'))
            LibPngCreate(file, image.mem.addr, image.info.width, image.info.height, (LibPngType)image.info.format);
    }
    else
        LibPngCreate(file, image.mem.addr, image.info.width, image.info.height, (LibPngType)image.info.format);
}
